/**
 * Created by wrl on 2017/7/22.
 */
var basePath = baseConfig.basePath;
$(function(){
    //图片验证码加载
    $('#VerityCodeImage').attr('src',basePath+'account/code');

    $("#accountCode").focus();

    var msg = {
        accountCodeMsg:'账号不能为空',
        accountPwdMsg:'密码不能为空',
        codeMsg:'请填写验证码'
    };
    //获取焦点，清除提示信息
    $('.input-text').on('focus',function(){
        var spanNode = $(this).parent().parent().find('.error-msg');
        $(spanNode).html('<span style="margin-left: 17px;"></span>');
    });

    $('.input-text').on('blur',function(){
        var inputValue = $(this).val();
        if(inputValue == ''){
            var spanNode = $(this).parent().parent().find('.error-msg');
            var errMsg = '';
            switch ($(this).attr('name')){
                case 'accountCode': errmsg = msg.accountCodeMsg; break;
                case 'accountPwd': errmsg = msg.accountPwdMsg; break;
                case 'code': errmsg = msg.codeMsg; break;
            }
            $(spanNode).html('<span style="margin-left: 17px;">'+errmsg+'</span>');
        }
    });

    //登录
    $('#loginBtn').click(function(){
        if(checkIsEmpty()){
            var formData = $('.form-amdin-login').serialize();
            var urlPath = './account/amdinLogin';
            customUtils.ajaxByDefault('post',formData,urlPath,function(result){
                console.info(result);
                if(result.flag == true){
                    if(undefined != result.successMsg){
                        layer.msg(result.successMsg, {
                            icon: 6,
                            time: 1500
                        });
                    }
                    if(undefined != result.checkErrMsg){
                        layer.msg(result.checkErrMsg, {
                            icon: 6,
                            time: 1500
                        });
                    }
                    setTimeout(function(){
                        location.replace(result.page);
                    },500);
                }else{
                    if(undefined != result.failMsg){
                        layer.msg(result.failMsg, {
                            icon: 5,
                            time: 1500
                        });
                    }
                    if(undefined != result.checkErrMsg){
                        layer.msg(result.checkErrMsg, {
                            icon: 5,
                            time: 1500
                        });
                    }
                    $('#VerityCodeImage').attr('src',basePath+'account/code?timestamp='+new Date().getTime());
                }
            });
        }
    });

    //更换图片验证码
    $('#changImage').click(function(){
        $('#VerityCodeImage').attr('src',basePath+'account/code?timestamp='+new Date().getTime());
    });

    $('#rememberMe').click(function(){
        if($(this).attr('checked') == undefined){
            $(this).attr('checked',true);
            $(this).val(true);
        }else{
            $(this).removeAttr('checked');
            $(this).val(false);
        }
    });

    //二维码
    $('.qrcode').click(function(){
        console.log('二维码');
        var url = basePath+'account/qrcode?timestamp='+new Date().getTime();
        customUtils.ajaxByDefault('get',null,url,function(result){
            console.log(result);
            $('#loginform').empty();
            var $back = $('<a onclick="location.replace(location.href)">账号密码登录</a>');
            $back.css({
                'float': 'left',
                'font-size': '18px',
                'cursor': 'pointer',
                'margin': '-24px -83px 0 13px'
            });
            var $img = $('<img style="margin:auto;margin-top:-19px;display:block;height: 100%;" src="'+result.qrcode+'">');
            $('#loginform').append($back);
            $('#loginform').append($img);
            setInterval(function(){
                var wxCodeURL = basePath+'account/adminSuccess?timestamp='+new Date().getTime();
                $.getJSON(wxCodeURL,null,function(result){
                    console.log(result);
                    if(result.page != undefined){
                        location.replace(basePath+result.page);
                    }
                });
            },2000);
        });
    });
    $(document).keyup(function (e) {
        if(e.which==13){
            $('#loginBtn').click();
        }
    })

});

function checkIsEmpty(){
    var flag = true;
    var $input = $('.input-text');
    $input.each(function(index,item){
        if($(item).val() == ''){
            flag = false;
            $(item).focus();
            return false;
        }
    });
    return flag;
}